package com.app.service.algs;

import com.app.dao.mysql.YjDataMapper;
import com.app.domain.UploadParameter;
import com.app.domain.YjData;
import com.app.domain.yxtj.GroutingProcess;
import com.app.domain.yxtj.YxtjGrouting;
import com.app.domain.yxtj.YxtjInfoData;
import com.app.domain.yxtj.YxtjResultData;
import com.app.service.UploadParameterService;
import com.app.service.YjDataService;
import com.app.utils.MathUtil;
import com.app.utils.TimeHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class AlgsUploadService {
    @Autowired
    UploadParameterService uploadParameterService;
    @Autowired
    YjDataService yjDataService;
    @Autowired
    YjDataMapper yjDataMapper;
    public Map<String, Object> getGrouting(List<YjData> yjDataList, UploadParameter uploadParameter, String projectGUID, String sectionGUID, String sectionCode, String factoryCode, Map<String, Object> map) {
        YxtjGrouting grouting = new YxtjGrouting();
        grouting.setID(UUID.randomUUID().toString().replaceAll("-", ""));
        grouting.setProjectGUID(projectGUID);
        grouting.setSectionGUID(sectionGUID);
        grouting.setSectionCode(sectionCode);
        grouting.setFactoryCode(factoryCode);
        grouting.setBridge(uploadParameter.getEquipmentname());
        grouting.setBeamCode(yjDataList.get(0).getBridge_code());
        grouting.setLTType(uploadParameter.getLblx());
        grouting.setProjectPart(uploadParameter.getSgbw());
        grouting.setEquipmentMaker("开封齐力");
        grouting.setEquipmentType("QLZY-II-300");
        grouting.setEquipmentCode("2023042701");
        grouting.setZKBYL(Float.valueOf(uploadParameter.getPumppressure()));
        grouting.setTensionTime(yjDataList.get(0).getMudjack_date());
        grouting.setStartTime(yjDataList.get(0).getMudjack_date() + " " + map.get("beginTime"));
        grouting.setEndTime(yjDataList.get(0).getMudjack_date() + " " + map.get("endTime"));
        grouting.setSGUnit(uploadParameter.getSgdw());
        grouting.setJLUnit(uploadParameter.getJldw());
        grouting.setSort(1);
        grouting.setCount(yjDataList.size());
        grouting.setMode(uploadParameter.getYajiangmoshi());
        grouting.setStatus("End");
        grouting.setDirection(uploadParameter.getYajiangfangxiang());
        grouting.setStep(uploadParameter.getYajiangbuzhou());
        grouting.setEnvironmentTemp(String.valueOf(yjDataList.get(0).getEnvironment_temperature()));
        grouting.setGroutTemp(String.valueOf(yjDataList.get(0).getSlurry_temperature()));
        grouting.setSGWorker("");
        grouting.setQAWorker("");
        grouting.setJLWorker("");
        grouting.setPrincipal("");
        grouting.setRecorder("");
        grouting.setIsWarning(1);
        grouting.setYJLOK(true);
        grouting.setSJBOK(true);
        grouting.setYJWJOK(true);
        List<GroutingProcess> groutingProcessList = new ArrayList<>();
        List<YxtjInfoData> yxtjInfoDataList = getInfoData(yjDataList.get(0).getBridge_code(), uploadParameter);
        Map<String, List> maps = getResultData(yjDataList, uploadParameter, projectGUID, sectionGUID, sectionCode, factoryCode);
        grouting.setInfoDataList(yxtjInfoDataList);
        grouting.setResultDataList(maps.get("resultData"));
        Map<String, Object> map1 = new HashMap<>();
        map1.put("grouting", grouting);
        map1.put("process", maps.get("process"));
        return map1;
    }
    private List<YxtjInfoData>  getInfoData(String bridge_code, UploadParameter uploadParameter) {
        List<YxtjInfoData> yxtjInfoDataList = new ArrayList<>();
        YxtjInfoData infoData = new YxtjInfoData();
        infoData.setID(UUID.randomUUID().toString().replaceAll("-", ""));
        infoData.setSNPZ("常规水泥");
        infoData.setSNBH("常规");
        infoData.setSNCJ("常规");
        infoData.setJBTime(180);
        infoData.setSJB(0.3f);
        infoData.setSNYL(100f);
        infoData.setSYL(30f);
        infoData.setWJJName("");
        infoData.setWJJYL(0f);
        infoData.setSNJQD_Design(0f);
        infoData.setSNJQD_Actual(0f);
        infoData.setSNJCD_Design(0f);
        infoData.setSNJCD_Actual(0f);
        infoData.setSNJLDD_Design(0f);
        infoData.setSNJLDD_Actual(0f);
        infoData.setZDQSL_Design(0f);
        infoData.setZDQSLActual(0f);
        infoData.setSNBH_3H_Design(0f);
        infoData.setSNBH_3H_Actual(0f);
        infoData.setSNBH_24H_Design(0f);
        infoData.setSNBH_24H_Actual(0f);
        yxtjInfoDataList.add(infoData);
        return yxtjInfoDataList;
    }
    private Map<String, List> getResultData(List<YjData> yjDataList, UploadParameter uploadParameter, String projectGUID, String sectionGUID, String sectionCode, String factoryCode) {
        List<YxtjResultData> resultDataList = new ArrayList<>();
        Map<String, List> map = new HashMap<>();
        for (YjData yjData : yjDataList) {
            YxtjResultData yxtjResultData = new YxtjResultData();
            yxtjResultData.setID(UUID.randomUUID().toString().replaceAll("-", ""));
            yxtjResultData.setHoleCode(yjData.getDuct_code());
            yxtjResultData.setStartTime(yjData.getMudjack_date() + " " + yjData.getBegin_time());
            yxtjResultData.setEndTime(yjData.getMudjack_date() + " " + yjData.getEnd_time());
            yxtjResultData.setJJYL(yjData.getMudjack_pressure());
            yxtjResultData.setCJYL(yjData.getSlurry_pressure());
            yxtjResultData.setWYWL(yjData.getStabilize_pressure());
            yxtjResultData.setZYJL(yjData.getPractical_mudjack_value());
            yxtjResultData.setBCSJ(Integer.valueOf(yjData.getPressurize_time()));
            yxtjResultData.setSJB(yjData.getWater_binder_ratio());
            yxtjResultData.setLLJL(yjData.getTheory_volume());
            yxtjResultData.setSCJL(yjData.getPractical_volume());
            yxtjResultData.setYJLVol(yjData.getPractical_mudjack_value());
            yxtjResultData.setWatervol(yjData.getPractical_water_value());
            yxtjResultData.setYJCS(Float.valueOf(yjDataList.size()));
            yxtjResultData.setYJLOK(true);
            yxtjResultData.setSJBOK(true);
            yxtjResultData.setYJWYOK(true);
            yxtjResultData.setISOK(true);
            yxtjResultData.setMemo("");
            yxtjResultData.setDatalD(yjData.getId());
            resultDataList.add(yxtjResultData);
            map.put("resultData", resultDataList);
            Map<String, List> graphData = yjDataService.getGraphData(String.valueOf(yjData.getId()));
            List<String> dateList = graphData.get("dateList");
            List<String> mcgList = graphData.get("mcgList");
            List<GroutingProcess> groutingProcessList = new ArrayList<>();
            for (int i = 0; i < mcgList.size(); i++) {
                GroutingProcess process = new GroutingProcess();
                process.setID(UUID.randomUUID().toString().replaceAll("-", ""));
                process.setGroutingResultID(yxtjResultData.getID());
                process.setProjectGUID(projectGUID);
                process.setSectionGUID(sectionGUID);
                process.setSectionCode(sectionCode);
                process.setFactoryCode(factoryCode);
                process.setBridge(uploadParameter.getEquipmentname());
                process.setBeamCode(yjData.getBridge_code());
                process.setHoleCode(yjData.getDuct_code());
                process.setDateTime(yjData.getMudjack_date() + " " + dateList.get(i));
                process.setJJYL(Float.valueOf(mcgList.get(i)));
                process.setCJYL(0f);
                process.setJJL(0f);
                process.setCJL(0f);
                process.setBYSJ(5);
                groutingProcessList.add(process);
            }
            map.put("process", groutingProcessList);
        }
        return map;
    }

    public String save(String deviceCode, String mudjackDate) {
        String startTime = null;
        String maxEndTime = yjDataMapper.findMaxEndTime(deviceCode, mudjackDate);
        List<YjData> yjDataList = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            YjData yjData = new YjData();
            yjData.setBridge_code("");
            yjData.setDevice_code(deviceCode);
            yjData.setDuct_code("N" + i);
            yjData.setMudjack_date(mudjackDate);
            if (i == 1) {
                if (maxEndTime != null) {
                    long j = (long) (900000 + Math.random() * (600000 - 900000 + 1));
                    String startDate = TimeHelper.timeStamp2Date(String.valueOf(TimeHelper.getTimestamp(mudjackDate + " " + maxEndTime, "yyyy-MM-dd HH:mm:ss") + j), "yyyy-MM-dd HH:mm:ss");
                    startTime = startDate.split(" ")[1];
                } else {
                    startTime = TimeHelper.randomDate("07:00:00", "08:00:00");
                }
            }

            yjData.setBegin_time(startTime);
            long j = (long) (90000 + Math.random() * (150000 - 90000 + 1));
            yjData.setEnd_time(TimeHelper.timeStamp2Date(String.valueOf(TimeHelper.getTimestamp(mudjackDate + " " + startTime, "yyyy-MM-dd HH:mm:ss") + j), "yyyy-MM-dd HH:mm:ss").split(" ")[1]
            );

            yjData.setMudjack_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
            yjData.setSlurry_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
            yjData.setStabilize_pressure(MathUtil.getRandomFloat(0.5f, 0.7f));
            yjData.setPressurize_time("300");
            yjData.setWater_binder_ratio(0.28f);
            yjData.setTheory_volume(100f);
            yjData.setPractical_volume(100f);
            yjData.setPractical_mudjack_value(41.6f);
            yjData.setPractical_water_value(58.1f);
            yjData.setEnvironment_temperature(27f);
            yjData.setSlurry_temperature(21f);
            yjData.setPractical_fluidity("16");
            yjData.setRemark("");
            yjData.setMudjack_curve_graph("");
            yjData.setState("2");
            long jj = (long) (120000 + Math.random() * (300000 - 120000 + 1));
            startTime = TimeHelper.timeStamp2Date(String.valueOf(TimeHelper.getTimestamp(mudjackDate + " " + startTime, "yyyy-MM-dd HH:mm:ss") + jj), "yyyy-MM-dd HH:mm:ss").split(" ")[1];
//            System.out.println(yjData);
            yjDataList.add(yjData);
        }

        String s;
        try {
            for (YjData yjData : yjDataList) {
                yjDataMapper.save(yjData);
            }
            s = "保存成功";
        } catch (Exception e) {
            s = "保存失败";
            e.printStackTrace();
        }
        return s;
    }
}
